<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Console\Commands;

use App\Enums\UserIsLocalEnum;
use App\Models\UserPosition;
use Illuminate\Console\Command;

class FillUserPositionIsLocal extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'fill:user_position';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '补充用户定位信息';

    /**
     * Create a new command instance.
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $positions = UserPosition::query()->get();
        /** @var UserPosition $position */
        foreach ($positions as $position) {
            $isLocal = checkInLocation($position->latitude, $position->longitude) ? UserIsLocalEnum::IN_LOCAL : UserIsLocalEnum::NOT_LOCAL;
            $position->is_local = $isLocal;
            $position->save();
        }
        return 0;
    }
}
